package com.azunitech.jms.ejb.unit;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


import com.google.inject.Inject;

public class RoleValidationInterceptor implements MethodInterceptor {

    @Inject
    private UserManager userManager;

    public Object invoke(MethodInvocation invocation) throws Throwable {

        Role requiredRole = 
            invocation.getMethod().getAnnotation(RequiresRole.class).value();

        if( userManager.getCurrentUser() == null ||
            !userManager.getCurrentUser().getRoles().contains(requiredRole)) {

            throw new IllegalStateException("User requires role " + requiredRole);
        }

        return invocation.proceed();
    }
}